//
//  TLoginViewController.m
//  Tikeyc
//
//  Created by ways on 16/8/24.
//  Copyright © 2016年 tikeyc. All rights reserved.
//

#import "TLoginViewController.h"

#import "TBaseNavigationViewController.h"
#import "TSignUpViewController.h"
#import "TForgetPasswordViewController.h"

#import "TLoginButton.h"

#import "TLoginViewModel.h"




@interface TLoginViewController ()<UITextViewDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *userPhotoImgView;

@property (strong, nonatomic) IBOutlet UITextField *userNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *userPasswordTextField;

@property (strong, nonatomic) IBOutlet TLoginButton *loginButton;

- (IBAction)forgetPasswordButtonAction:(UIButton *)sender;

- (IBAction)signUpButtonAction:(UIButton *)sender;

@property (nonatomic,strong)TLoginViewModel *loginViewModel;

@end

@implementation TLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self initBindProperty];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


#pragma mark - set

- (TLoginViewModel *)loginViewModel{
    if (!_loginViewModel) {
        _loginViewModel = [[TLoginViewModel alloc] initLoginViewModel];
    }
    return _loginViewModel;
}

#pragma mark - init

- (void)initBindProperty{
    
    XGKeyValueItem * loginModel = (XGKeyValueItem *)[[XGFMDBTool sharedFMDBManager] getDBObjectById:TUSERINFO_TABLE_NAME_id fromTable:TUSERINFO_TABLE_NAME];
    self.userNameTextField.text = loginModel.itemResult[@"userName"];
    self.userPasswordTextField.text = loginModel.itemResult[@"userPassword"];
    
    @weakify(self)
    //
    RAC(self.loginViewModel.userInfoModel,userName) = [[self.userNameTextField rac_textSignal] map:^id(NSString *value) {
//        NSLog(@"%@",value);
        @strongify(self)
//        if (![[value lowercaseString] isEqualToString:@"tikeyc"]) {
//            self.userNameTextField.textColor = [UIColor redColor];
//        }else{
//            self.userNameTextField.textColor = [UIColor blackColor];
//        }
        return value;
    }];
    RAC(self.loginViewModel.userInfoModel,userPassword) = [[self.userPasswordTextField rac_textSignal] map:^id(NSString *value) {
        @strongify(self)
        if (value.length < 6) {
            self.userPasswordTextField.textColor = [UIColor redColor];
        }else{
            self.userPasswordTextField.textColor = [UIColor blackColor];
        }
        return value;
    }];
    //
    
    [[self.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self logInOpemIM];
        return;
        @strongify(self)
        [self.view endEditing:YES];
        self.view.userInteractionEnabled = NO;
        if (!self.loginViewModel.loginButtonEnable) {
            
            [self.loginButton errorRevertAnimationCompletion:^{
                self.view.userInteractionEnabled = YES;
            }];
        }else{
            
            self.view.userInteractionEnabled = NO;
            [self.loginButton startAnimation];
            [[self.loginViewModel.requestCommand execute:nil] subscribeNext:^(id x) {
                NSLog(@"execute...");
                if ([self.loginViewModel.userInfoModel.msgCode intValue] == 100) {//登录成功
                    [self logInOpemIM];
                } else {
                    [self.loginButton errorRevertAnimationCompletion:^{
                        self.view.userInteractionEnabled = YES;
                    }];
                    
                }
                
            }];
            
        }
        
    }];
    //
    [self.loginViewModel.loginButtonEnableSignal subscribeNext:^(id x) {
        
    }];
    
    RACSignal *didSignal = [TNotificationCenter rac_addObserverForName:UIKeyboardWillShowNotification object:nil];
    RACSignal *willSignal = [TNotificationCenter rac_addObserverForName:UIKeyboardWillHideNotification object:nil];
    RAC(self.view,top) = [[didSignal merge:willSignal] map:^id(id value) {
        if ([value name] == UIKeyboardWillHideNotification) {
            return @(0);
        }
        CGRect keyboardRect = [[[value userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGRect convertRect = [self.userPasswordTextField convertRect:self.userPasswordTextField.frame toView:self.view];
        NSLog(@"%@",NSStringFromCGRect(convertRect));
        if (convertRect.origin.y + convertRect.size.height > keyboardRect.origin.y) {
            NSLog(@"move");
            return @(keyboardRect.origin.y - (convertRect.origin.y + convertRect.size.height));
        }
        
        return @(0);
    }];
    
    
    
}

#pragma mark - OpenIM

- (void)logInOpemIM {
    [self.loginViewModel tryLoginOpenIMWithSuccessBlock:^{
        [self.loginButton exitAnimationCompletion:^{
            [TAppDelegateManager gotoMainController];
        }];
    } failedBlock:^(NSError *aError) {
        
    }];
    
    self.view.userInteractionEnabled = YES;
}

#pragma mark - Actions Method

- (IBAction)forgetPasswordButtonAction:(UIButton *)sender {
    
    TForgetPasswordViewController *forgetPWVC = [[TForgetPasswordViewController alloc] init];
    TBaseNavigationViewController *forgetPWVCNV = [[TBaseNavigationViewController alloc] initWithRootViewController:forgetPWVC];
    
    [self presentViewController:forgetPWVCNV animated:YES completion:^{
        
    }];
}

- (IBAction)signUpButtonAction:(UIButton *)sender {
    TSignUpViewController *signUpVC = [[TSignUpViewController alloc] init];
    TBaseNavigationViewController *signUpVCNV = [[TBaseNavigationViewController alloc] initWithRootViewController:signUpVC];
    @weakify(self)
    signUpVC.userNameBlock = ^(NSString *userName) {
        @strongify(self)
        self.userNameTextField.text = userName;
        self.loginViewModel.userInfoModel.userName = userName;
        self.userPasswordTextField.text = nil;
    };
    
    [self presentViewController:signUpVCNV animated:YES completion:^{
        
    }];
}

@end












